package com.jlastu.androiduidemo.views;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.jlastu.androiduidemo.R;
import com.jlastu.androiduidemo.database.QQHelper;
import com.jlastu.androiduidemo.database.QQperate;

import java.util.HashMap;
import java.util.Map;

public class Login extends AppCompatActivity implements View.OnClickListener {
    QQHelper qqHelper;
    private EditText login_z, login_m;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        qqHelper = new QQHelper(Login.this);
        init();
        Outdate();
    }

    private void init() {
        login_z = findViewById(R.id.login_z);
        login_m = findViewById(R.id.login_m);
        Button bt_register = findViewById(R.id.bt_register);
        Button bt_xpassword = findViewById(R.id.bt_xpassword);
        Button bt_login = findViewById(R.id.bt_login);
        bt_register.setOnClickListener(this);
        bt_xpassword.setOnClickListener(this);
        bt_login.setOnClickListener(this);
    }

    private void Outdate() {
        Map<String, String> map = getAccountInfo(); // 使用新方法获取账号信息
        String account = map.get("account");
        String password = map.get("password");
        login_z.setText(account);
        login_m.setText(password);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.bt_register) {
            Intent intent = new Intent(Login.this, Signin.class);
            startActivity(intent);
            finish();
        } else if (id == R.id.bt_xpassword) {
            Intent intent = new Intent(Login.this, UpdatePassword.class);
            startActivity(intent);
            finish();
        } else if (id == R.id.bt_login) {
            String account = login_z.getText().toString().trim();
            String password = login_m.getText().toString();
            Log.i("debug", "登录按钮");
            if (TextUtils.isEmpty(account) || TextUtils.isEmpty(password)) {
                Toast.makeText(this, "账号和密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            int f = QQperate.query(Login.this, account, password);
            if (f == 1) {
                saveAccountInfo(account, password); // 异步保存账号信息
                Intent intent = new Intent(Login.this, MainActivity.class);
                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                startActivity(intent);
                finish();
            } else {
                Toast.makeText(this, "账号或密码错误", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void saveAccountInfo(String account, String password) {
        SharedPreferences sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("account", account);
        editor.putString("password", password);
        editor.apply(); // 使用apply而不是commit，因为apply是异步的
    }

    public Map<String, String> getAccountInfo() {
        SharedPreferences sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE);
        String account = sharedPreferences.getString("account", "");
        String password = sharedPreferences.getString("password", "");
        Map<String, String> userInfo = new HashMap<>();
        userInfo.put("account", account);
        userInfo.put("password", password);
        return userInfo;
    }
}